#!/bin/bash
#
# ledmemo -- LED memory game
#
# Copyright (C) Anselm Lingnau <anselm@anselms.net>
#
# This game uses the LED to produce a sequence of coloured
# flashes. The user is expected to type in the sequence of
# colours. The game starts with a single flash, and after
# each correct guess another flash is appended to the sequence,
# up to a maximum of 10. If the user can reproduce a 10-element
# sequence correctly they win.

MAX=10
COLORS="rgb"

SHOW=0.7
SPACE=0.1

function blink() {
    #echo $1
    i=0
    while [[ $i -lt ${#1} ]]
    do
	led blink ${1:$i:1} $SHOW
	sleep $SPACE
	i=$((i+1))
    done
    echo ""
}

# Generate $MAX-item random sequence
seq=$(for i in $(seq 1 $MAX)
    do
	echo -n $((RANDOM % ${#COLORS}))
    done | tr 012 $COLORS)

# Loop over the sequence
led start

length=1
while [[ $length -le $MAX ]]
do
    task=${seq:0:$length}
    clear
    read -p "Länge: $length -- RETURN zum Starten"
    blink $task
    read -p "Farbenfolge eingeben ($COLORS): " in
    if [[ $in != $task ]]
    then
	break
    fi
    length=$((length+1))
done

led stop

echo ""
if [ $length -gt $MAX ]
then
    echo "Genial, Sie haben ein gutes Gedächtnis!"
else
    echo "Sorry, leider falsch ..."
fi
